package com.tobie.schedule.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.tobie.info.service.api.InfoApiService;
import com.tobie.schedule.pojo.entity.ScheduleEntity;
import com.tobie.schedule.service.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Random;

/**
 * @author Tobieance
 * @description
 * @date 2023-11-17 18:01
 */
@Service
@Slf4j
public class MailServiceImpl implements IMailService {

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private InfoApiService infoApiService;

    /**
     * 发送邮箱验证码
     *
     * @param mail 邮箱
     * @return boolean
     */
    @Override
    public boolean sendVerifyMail(String mail, String code) {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //主题
            mailMessage.setSubject("验证码邮件");
            //生成随机数
            //内容
            mailMessage.setText("您收到的验证码是：" + code);
            //发给谁
            mailMessage.setTo(mail);
            //自己的邮箱
            mailMessage.setFrom(from);
            //发送
            mailSender.send(mailMessage);
            log.info("接收邮箱: {}", mail);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取验证码
     *
     * @return {@link String}
     */
    @Override
    public String getVerifyCode() {
        Random random = new Random();
        StringBuilder randomCombination = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int randomNumberOrLetter = random.nextInt(36);
            if (randomNumberOrLetter < 10) {
                randomCombination.append(randomNumberOrLetter);
            } else {
                randomCombination.append((char) (randomNumberOrLetter + 55));
            }
        }
        return randomCombination.toString();
    }

    /**
     * 发送提醒邮件
     *
     * @param scheduleEntity 日程实体类
     * @return boolean
     */
    @Override
    public boolean sendAlertEmail(ScheduleEntity scheduleEntity, String mail) {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            StringBuilder text = new StringBuilder();
            //主题
            mailMessage.setSubject("您的预定到时间了");
            String type = "";
            Object typeContent=infoApiService.getTypeInfo(scheduleEntity.getScheduleType(),scheduleEntity.getScheduleContentId());
            switch (scheduleEntity.getScheduleType()) {
                case 1:
                    type = "航班";
                    break;
                case 2:
                    type = "酒店";
                    break;
                case 3:
                    type = "租车";
                    break;
                case 4:
                    type = "景点";
                    break;
                case 5:
                    type = "套餐";
                    break;
                default:
                    type = "";
                    break;
            }
            text.append("您预定的：").append(type).append(" 快要到时间了\n");
            text.append("详细信息如下：\n").append(JSONObject.parse(typeContent.toString()));
            mailMessage.setText(text.toString());
            //发给谁
            mailMessage.setTo(mail);
            //自己的邮箱
            mailMessage.setFrom(from);
            //发送
            mailSender.send(mailMessage);
            log.info("接收邮箱:{}", mail);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
